package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * JDBC java数据库链接
 */
public class JDBCDemo1 {
    public static void main(String[] args) {
         /*
    使用JDBC链接数据库的大致流程：
    1：加载驱动
    2：使用DriverManager(JDBC提供的)
     */
        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2使用
            Connection connection=DriverManager.getConnection(
                    //jdbc:mysql://localhost:3306/
                    "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root");
            System.out.println("连接数据库成功");
            Statement statement=connection.createStatement();
            String sql="create table user(\n" +
                    "    id int primary key auto_increment,\n" +
                    "    username varchar(30),\n" +
                    "    password varchar(30),\n" +
                    "    nickname varchar(30),\n" +
                    "    age int(3)\n" +
                    ");";
            statement.execute(sql);
            System.out.println("创建表成功");
            connection.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
